﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yd.Util;
using System.IO;
using Yd.CommCls;

namespace yedian.User
{
    public partial class uploadPhoto : PBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            bool success = false;
            string msg = "";
            double size = 2;
            //Response.se.setContentType("text/xml; charset=utf-8");
            //Response.HeaderEncoding("utf-8");
            //Response.HeaderEncoding = System.Text.Encoding.UTF8;
            Response.ContentEncoding = System.Text.Encoding.UTF8;

            //HttpSessionContext SessCon = request.getSession(false).getSessionContext();
            //HttpSession Sess = SessCon.getSession(SessionId);




            HttpPostedFile oFile = Request.Files["Filedata"];
            if (oFile == null || oFile.ContentLength < 1)
            {
                //string js = "window.parent.document.getElementById('preTitle{0}').innerText = '上传失败';this.location.href='about:blank';";

                //Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(js, value), true);
                string json = "{\"success\":\"false\",\"msg\":\"上传失败！\"}";
                Response.Write(json);
            }
            else if (oFile.ContentLength > size * 1024 * 1024)
            {
                string json = "{\"success\":\"false\",\"msg\":\"上传文件不能大于" + size + "M\"}";
                Response.Write(json);
            }
            else
            {
                try
                {
                    string filePath = "";
                    string _f1 = "";
                    if (oFile != null)
                    {
                        _f1 = CommUtils.UpfilePic(oFile, Yd.CommCls.CommUtils._filepic);
                    }
                    if (_f1.IndexOf("不正确") != -1)
                    {
                        msg = _f1;
                        //er.Text = "<font color='red'>正面身份证照片错误！" + _f1 + "</font>";
                        //return;
                    }
                    else
                    {
                        success = true;
                        msg = "{\"success\":\"false\",\"msg\":\"上传成功！\"}";
                        filePath = "{\"success\":\"true\",\"msg\":\"上传成功！\",\"filePath\":\"" + _f1 + "\"}";
                    }


                    string json = "";
                    if (success)
                    {
                        json = filePath;

                    }
                    else
                    {
                        json = msg;
                    }
                    Response.Write(json);

                    //string cFileName = GetRandomFileName(oFile.FileName);
                    //Stream fs = oFile.InputStream;

                    //byte[] by = new byte[1024];//分块读取

                    //string jsLoad = "";

                    //string folderPath = Server.MapPath("~/UploadFloder/");
                    //string filePath = folderPath + cFileName;
                    //if (!Directory.Exists(folderPath))
                    //{
                    //    Directory.CreateDirectory(folderPath);
                    //}

                    //FileStream fStream = new FileStream(filePath, FileMode.Create);
                    //int osize = fs.Read(by, 0, by.Length);
                    //int totalNum = (int)fs.Length / by.Length;
                    //totalNum++;
                    //int i = 0;

                    //while (osize > 0)
                    //{

                    //    if (osize > 0)
                    //    {
                    //        fStream.Write(by, 0, osize);
                    //        string msg = "已经上传" + osize + "数据";
                    //        //MyProgressBar.Roll(msg, ((i + 1) * 100 / totalNum), value);
                    //    }
                    //    osize = fs.Read(by, 0, by.Length);
                    //    //Thread.Sleep(10000);
                    //    jsLoad = "已经上传" + osize + "数据";

                    //    //fStream.Close();
                    //    //fStream.Dispose();
                    //    //string jsBlock = "<script language=\"javascript\">window.parent.document.getElementById('preTitle" + value + "').innerText = '上传成功';window.parent.Jui.uploadObject.uploadNext();</script>";

                    //    //System.Web.HttpContext.Current.Response.Write(jsBlock);
                    //    //System.Web.HttpContext.Current.Response.Flush();
                    //}
                    //fStream.Close();
                    //fStream.Dispose();
                    ////string jsBlock = "<script language=\"javascript\">window.parent.document.getElementById('preTitle" + value + "').innerText = '上传成功';window.parent.Jui.uploadObject.uploadNext();</script>";

                    ////System.Web.HttpContext.Current.Response.Write(jsBlock);
                    ////System.Web.HttpContext.Current.Response.Flush();
                    //string json = "{\"success:\":\"true\",\"msg\":\"上传成功！\",\"filePath\":\"" + cFileName + "\"}";
                    //Response.Write(json);
                }
                catch (Exception ee)
                {
                    string json = "{\"success\":\"false\",\"msg\":\"" + ee.Message + "！\"}";
                    Response.Write(json);
                    //string js = "window.parent.document.getElementById('preTitle{0}').innerText = '上传失败';this.location.href='about:blank';";
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(js, value), true);
                }

            }
        }

        /// <summary>
        /// 取得随机文件名
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetRandomFileName(string filename)
        {
            string[] files = filename.Split('.');
            string exfilename = "." + files.GetValue(files.Length - 1);

            char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9','A' 
          ,'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q' 
          ,'R','S','T','U','V','W','X','Y','Z'};
            string num = "";
            Random r = new Random();
            for (int i = 0; i < 4; i++)
                num += s[r.Next(0, s.Length)].ToString();
            DateTime time = DateTime.Now;
            string name = time.Year.ToString()
                + time.Month.ToString().PadLeft(2, '0')
                + time.Day.ToString().PadLeft(2, '0')
                + time.Hour.ToString().PadLeft(2, '0')
                + time.Minute.ToString().PadLeft(2, '0')
                + time.Second.ToString().PadLeft(2, '0')
                + num + exfilename;
            return name;
        }
    }
}